package com.liu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.eduservice.entity.EduCourse;
import com.liu.eduservice.entity.EduCourseDescription;
import com.liu.eduservice.entity.vo.CourseInfoForm;
import com.liu.eduservice.entity.vo.CoursePublishVo;
import com.liu.eduservice.entity.vo.CourseQuery;
import com.liu.eduservice.mapper.EduCourseMapper;
import com.liu.eduservice.service.EduChapterService;
import com.liu.eduservice.service.EduCourseDescriptionService;
import com.liu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.eduservice.service.EduVideoService;
import com.liu.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author achang
 * @since 2022-05-20
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {


    // 调用EduCourseDescriptionService
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    @Override
    public String saveCourseInfo(CourseInfoForm courseInfoForm) {

        // 1.将前端传递的数据对象，保存到EduCourse表中
        EduCourse eduCourse = new EduCourse();
        // 将前端数据对象courseInfoForm的所有属性值，直接赋值给eduCourse
        BeanUtils.copyProperties(courseInfoForm, eduCourse);
        try {
            int insert = baseMapper.insert(eduCourse);
            if (insert <= 0) {
                throw new GuliException(20001, "系统正忙，请稍后再试！");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        // 如果插入成功后，将eduCourse的id值取出，同样赋值给eduCourseDescription的id
        // 保证两张表的id值是同一个
        String eduCourseId = eduCourse.getId();


        // 2.将前端数据对象的部分值，传递给另一张表EduCourseDescription
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        eduCourseDescription.setId(eduCourseId);
        try {
            boolean save = eduCourseDescriptionService.save(eduCourseDescription);
            if (!save) {
                throw new GuliException(20001, "系统正忙，请稍后再试！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return eduCourseId;

    }

    // 根据课程id查询课程
    @Override
    public CourseInfoForm getCourseInfo(String courseId) {
        // 查询课程信息表EduCourse
        EduCourse eduCourse = baseMapper.selectById(courseId);


        // 查询课程信息描述表EduCourseDescription
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(courseId);

        // 封装成对象CourseInfoForm
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        BeanUtils.copyProperties(eduCourse, courseInfoForm);
        courseInfoForm.setDescription(eduCourseDescription.getDescription());
        return courseInfoForm;
    }

    @Override
    public void updateCourseInfo(CourseInfoForm courseInfoForm) {
        //1、修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm, eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if (update <= 0) {
            throw new GuliException(20001, "修改课程信息失败");
        }

        //2、修改描述信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        eduCourseDescription.setId(courseInfoForm.getId());
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }

    //根据课程id查询课程确认信息
    @Override
    public CoursePublishVo getPublishCourseInfo(String courseId) {
        return baseMapper.getPublishCourseInfo(courseId);
    }

    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {

        //构建条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        //取出值，判断他们是否有值
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();

        //判断条件值是否为空，如果不为空，拼接条件
        //判断是否有传入教师名
        if (!StringUtils.isEmpty(title)) {
            //构建条件
            wrapper.like("title", title);//参数1：数据库字段名； 参数2：模糊查询的值
        }
        //判断是否传入状态
        if (!StringUtils.isEmpty(status)) {
            //构造条件
            wrapper.eq("status", status);
        }

        //排序
        wrapper.orderByDesc("gmt_create");

        //带上门判断后的条件进行分页查询
        baseMapper.selectPage(pageParam, wrapper);
    }

    // 课程列表中删除课程方法
    @Override
    public void removeCourse(String courseId) {
        // 第一步，根据课程id删除小节
        eduVideoService.removeVideoByCourseId(courseId);
        // 第二步，根据课程id删除章节
        eduChapterService.removeChapterByCourseId(courseId);

        // 第三步，根据课程id删除课程描述(课程表和课程描述表示一对一的关系，也就是主键id是相同的)
        try {
            boolean b = eduCourseDescriptionService.removeById(courseId);
            if (!b) {
                throw new GuliException(20001, "删除课程描述失败！");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        // 第四步，根据课程id删除课程
        try {
            int i = baseMapper.deleteById(courseId);
            if (i <= 0) {
                throw new GuliException(20001, "删除课程失败！");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

    }

}
